<template>
  <div class="article-comments">
    <!-- 导航栏 -->
    <van-nav-bar title="评论回复">
      <van-icon slot="left"
                name="cross" />
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 当前评论 -->
    <van-cell title="当前评论">
      <van-image slot="icon"
                 round
                 width="30"
                 height="30"
                 style="margin-right: 10px;"
                 :src="currentComment.aut_photo" />
      <span style="color: #466b9d;"
            slot="title">{{currentComment.aut_name}}</span>
      <div slot="label">
        <p style="color: #363636;">{{currentComment.content}}</p>
        <p>
          <span style="margin-right: 10px;">{{currentComment.pubdate | getRelativeTime}}</span>
          <van-button size="mini"
                      type="default">回复 {{currentComment.reply_count}}</van-button>
        </p>
      </div>
      <van-icon slot="right-icon" />
    </van-cell>
    <!-- /当前评论 -->
    <van-divider>全部回复</van-divider>

    <!-- 对当前评论 回复列表 -->
    <van-list v-model="loading"
              :finished="finished"
              finished-text="没有更多了"
              @load="onLoad">
      <van-cell v-for="(item, index) in list"
                :key="index">
        <van-image slot="icon"
                   round
                   width="30"
                   height="30"
                   style="margin-right: 10px;"
                   :src="item.aut_photo" />
        <span style="color: #466b9d;"
              slot="title">{{item.aut_name}}</span>
        <div slot="label">
          <p style="color: #363636;">{{item.content}}</p>
          <p>
            <span style="margin-right: 10px;">{{item.pubdate | getRelativeTime}}</span>
          </p>
        </div>
      </van-cell>
    </van-list>
    <!-- 回复列表 -->

    <!-- 发布回复 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable
                 placeholder="请输入回复内容">
        <van-button slot="button"
                    size="mini"
                    type="info">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布回复 -->
  </div>

</template>
<script>
import { reqGetArticleComments } from '@/api/comments'
export default {
  name: 'CommentReply',
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束\
      offset: null
    }
  },
  props: {
    currentComment: { // 接收父组件传递过来的评论项
      type: Object,
      required: true
    }
  },
  created () {
    console.log('创建弹层内容')
  },
  methods: {
    async onLoad () {
      // 异步更新数据
      const data = await reqGetArticleComments('c', this.currentComment.com_id.toString(), this.offset)
      // console.log(data)
      const results = data.data.results
      // this.list.push(...results)
      this.list = [...this.list, ...results]
      // 赋值offset
      this.offset = data.data.last_id
      // 关闭loading
      this.loading = false
      // 数据请求完毕
      if (results.length === 0) {
        this.finished = true
      }
    }
  }
}
</script>
<style lang="scss" scoped>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
.van-nav-bar .van-icon {
  color: #fff;
}
</style>
